Skip to content

[2.4.x] mod_proxy_beacon: Backport the module and the mod_proxy changes it needs from trunk - #994

Open
jimjag wants to merge 10 commits into
apache:2.4.xfrom
jimjag:backport-mod_proxy_beacon
Open

jimjag wants to merge 10 commits into
apache:2.4.xfrom
jimjag:backport-mod_proxy_beacon

Conversation

@jimjag

@jimjag jimjag commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Backports mod_proxy_beacon from trunk, with the mod_proxy changes it builds on: the PROXY_WORKER_BEACON status bit (MMN minor 143), atomic updates of the shared worker status, no half-defined worker left behind when ap_proxy_define_worker() fails, and run-time balancer workers added under the balancer locks. It also keeps mod_proxy_hcheck's hash of health check workers out of pconf, where its watchdog filled it without a lock while other threads grew balancers there. This replaces three STATUS proposals: mod_proxy_beacon, "Update the shared worker status word atomically" and "Don't leave a half-defined worker".

The module is opt-in. On 2.4.x the proxy config.m4 limits the module selection to most, so even reallyall leaves it out; it needs --enable-proxy-beacon, which the "Python pytest test suites" job now passes so that the beacon tests already on 2.4.x actually run. It needs apr_siphash.h, so configure refuses it with APR-util older than 1.6.

Trunk: r1934995 r1935040 r1935556 r1938135 r1938912 r1939349 r1939350 r1939351 r1939352 r1939362 r1939364 r1939391 r1939436 r1939481 r1939482 r1939484 r1939487 r1939493, 9faada1 6e5c70b, and the docs commits a6d8112 and 1624d94.

jimjag added a commit that referenced this pull request Nov 10, 2026
@ylavic

ylavic commented Nov 25, 2026

Copy link
Copy Markdown
Member

See #1084, would need to go here or just before/after it.

@ylavic

ylavic commented Nov 27, 2026

Copy link
Copy Markdown
Member

@jimjag For the 2.4.x side of #1084: 2.4.x is already exposed to that crash (children crashing when workers are added at run time) through the balancer-manager, beacon or not.

I'd propose a separate backport of the fixes, taking from this PR your generic mod_proxy/balancer-manager changes (define-worker rollback, atomic worker status, run-time workers complete and under the locks), plus the locking part of your hcheck fix, #1084 and #1103 (proxy_mutex as a per-child thread mutex). This PR would then rebase on it and keep beacon alone (plus #1084's beacon part); I can do the rebase here if you want.
Or, if you prefer to keep it all in one backport, I can cherry-pick those here instead, on top of yours.
Which would you prefer?

Also #1105 restricts b_nwrkr_flags to beacon's 'B' through balancer_manage() and drops it from balancer-manager requests; if you agree with the change, I think it would belong here too.

Nit: the docs here for mod_proxy_beacon say 2.4.71 (we are now at 2.4.72).

@notroj

notroj commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Still seeing trunk crashes tickled by mod_proxy_beacon, e.g. here, Claude suggests this is because multiple threads are still allocating directly out of pconf:

Two threads allocate from pconf at runtime with no common lock:

  • Health-check thread: ctx->hcworkers is created with apr_hash_make(p) in hc_create_config(), where p is pconf. Every apr_hash_set() in hc_get_hcworker() allocates an entry from pconf, and so does the bucket array when the hash grows past 15 entries. It holds no lock while doing so.
  • Beacon thread: balancer->workers is created in ap_proxy_define_balancer(cmd->pool, ...), again pconf. ap_proxy_sync_balancer() calls apr_array_push(b->workers), which reallocates from pconf when the array grows (at 20 members here). It holds only the proxy mutex.

@jimjag

jimjag commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

@ylavic It's likely best to have a everything "squashed" together as long as they are tightly linked. So a rebase makes sense.

Comment thread modules/proxy/mod_proxy_beacon.c Outdated
if (!port) {
port = match->bind_addr->port;
}
if (apr_sockaddr_ip_get(&ip, match->bind_addr) == APR_SUCCESS) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also allocates from the process pool which looks unsafe in a watchdog thread, safer to use _ip_getbuf

* gated behind APU_HAVE_CRYPTO, which isn't always built; this is the same
* branchless idiom. */
static int beacon_const_time_eq(const void *a, const void *b, apr_size_t n)
{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a duplicate of ap_memeq_timingsafe() which is also already in 2.4.x

jimjag and others added 5 commits November 29, 2026 08:35
The watchdog adds to it without a lock while other threads grow
balancers' workers arrays in pconf, and children crashed on the pool.

(cherry picked from commit 9faada1)
Pointed out by Joe Orton.

(cherry picked from commit 6e5c70b)
…er walks (apache#1084)

* mod_proxy: define run-time workers under proxy_mutex.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerAddWorkers): New test.

* mod_proxy: walk the workers of a balancer under its thread lock.

* modules/proxy/proxy_util.h:
  (PROXY_WORKERS_LOCK, PROXY_WORKERS_UNLOCK): New, the balancer's thread lock
  unless ungrowable.
  (ap_proxy_balancer_snap_workers): Declare.

* modules/proxy/proxy_util.c:
  (ap_proxy_balancer_snap_workers): New, the workers to walk.
  (ap_proxy_location_reverse_map): Walk a balancer's workers under its lock,
  unless ungrowable.

* modules/proxy/mod_proxy_balancer.c:
  (get_balancer_worker): New helper, look up a worker under the balancer's
  lock.
  (mark_ungrowable_balancers): New, mark the balancers that can't grow.
  (balancer_post_config): Call it.
  (recalc_factors): Take the balancer's lock.
  (balancer_manage, balancer_handler): Look up the worker with
  get_balancer_worker().
  (balancer_display_page): Walk snapshots of the workers arrays.

* modules/proxy/mod_proxy.c:
  (proxy_status_hook): Walk the snapshot of each balancer's workers.

* modules/proxy/mod_proxy.h:
  (proxy_balancer): Add ungrowable.
  (ap_proxy_get_worker_ex, ap_proxy_get_worker): Document the lock for a
  lookup in a balancer.

* include/ap_mmn.h:
  Minor bump for proxy_balancer's ungrowable.

* mod_proxy_balancer: share one thread lock across the vhost copies of a balancer.

* modules/proxy/mod_proxy_balancer.c:
  (balancer_child_init): Share the thread lock of the copies of a growable
  balancer.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerVhostCopies): New test.

* test: allow AH01171 in test_proxy_beacon.py.

* test/pyhttpd/classic_errorlog.py:
  (PER_FILE): Allow AH01171 for test_proxy_beacon.py.

* test: walk the workers of balancers that can't grow, and persisted ones.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerNoGrowth, TestBalancerMixedGrowth, TestBalancerPersist): New
  tests.

* mod_proxy: don't add a worker to an ungrowable balancer at run time.

* modules/proxy/proxy_util.c:
  (ap_proxy_sync_balancer): Don't add a worker to an ungrowable balancer.
* modules/proxy/mod_proxy_balancer.c:
  (balancer_add_worker): Likewise.
* modules/proxy/mod_proxy.h:
  (struct proxy_balancer): Say what enforces ungrowable.

(cherry picked from commit 557b62c)
Marked as backported in 2.4.72, with the fixes.
@jimjag
jimjag force-pushed the backport-mod_proxy_beacon branch from ff053dc to c7f951a Compare November 29, 2026 14:43
ylavic and others added 5 commits November 29, 2026 09:46
proxy_mutex serializes the definition, initialization and sync of the
run-time workers.  Those are per-process data, allocated from per-process
pools, so a cross-process lock is not needed.  Create it as a thread mutex
in pconf at post_config, for each child to inherit unlocked, and no longer
init it in child_init.  The "proxy" Mutex type stays registered so that
existing Mutex directives naming it are still accepted; they have no
effect.

* modules/proxy/mod_proxy.c (proxy_mutex): Move to proxy_util.c.
  (proxy_post_config): Create it with proxy_util_post_config().
  (child_init): Drop apr_global_mutex_child_init().
  (proxy_pre_config): Say why the "proxy" type is still registered.

* modules/proxy/proxy_util.c (proxy_mutex): A static thread mutex, with
  APR_HAS_THREADS only.
  (proxy_util_post_config): New, creates it.
  (proxy_mutex_lock, proxy_mutex_unlock): New helpers, no-ops without
  APR_HAS_THREADS.
  (ap_proxy_define_worker_ex, ap_proxy_initialize_worker,
  ap_proxy_sync_balancer): Use them.

* modules/proxy/proxy_util.h (proxy_util_post_config): New internal
  declaration.

* docs/manual/mod/core.xml (Mutex): List the "proxy" mutex, without
  effect.

* changes-entries/proxy-mutex-thread.txt: New entry.

(cherry picked from commit 296a4e6)
Without APR_HAS_THREADS, PROXY_THREAD_LOCK/UNLOCK() (and so
PROXY_WORKERS_LOCK/UNLOCK()) expanded to a constant, which used as a
statement is a "statement with no effect", and some status variables were
undeclared or unused.

* modules/proxy/mod_proxy.h (PROXY_THREAD_LOCK, PROXY_THREAD_UNLOCK): Call
  the internal inline ap_proxy__nolock() returning APR_SUCCESS without
  threads.

* modules/proxy/proxy_util.h (PROXY_WORKERS_LOCK, PROXY_WORKERS_UNLOCK):
  Same as PROXY_THREAD_LOCK/UNLOCK() without threads.

* modules/proxy/proxy_util.c (ap_proxy_initialize_balancer): Declare rv
  with or without threads.

* modules/proxy/mod_proxy_balancer.c (find_best_worker,
  proxy_balancer_pre_request, proxy_balancer_post_request): Declare rv
  with threads only.

(cherry picked from commit d80ad57)
* docs/manual/mod/core.xml (Mutex): Add the proxy-balancer-shm mutex of
  mod_proxy_balancer.

(cherry picked from commit ced45d3)
…pache#1105)

A worker added through the balancer-manager is now created disabled with
no other status flag: b_nwrkr_flags is dropped from its requests.
balancer_manage()'s callers can still create a worker marked 'B'
(mod_proxy_beacon), but an add asking for any other flag is refused.

* modules/proxy/mod_proxy_balancer.c
  (balancer_handler): Drop b_nwrkr_flags from the request's parameters.
  (balancer_process_balancer_worker): Refuse an add with a status flag
  other than PROXY_WORKER_BEACON_FLAG.

* test/modules/proxy/test_12_balancer.py: Check that a worker added with
  b_nwrkr_flags through the balancer-manager is created without them.

(cherry picked from commit bbcf435)
The locking part of trunk 84cc989: the watchdog synced and walked the
workers arrays without the lock that request threads grow them under.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants